/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */

package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.ouertech.android.agm.lib.base.utils.UtilMD5;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.enums.ESmsType;
import com.simon.mengkou.future.base.OuerException;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @author : Zhenshui.Xia
 * @date   :  2015-4-23
 * @desc   : (注册or忘记密码）密码设置界面
 */
public class UserPwdActivity extends BaseTopActivity {
	//注册：1，忘记密码：2
	private int mType;
	//手机号码
	private String mPhone;
	//验证码
	private String mCode;

	//密码
	@Bind(R.id.pwd_id_pwd)
	EditText mEtPasswd;
	//确认密码
	@Bind(R.id.pwd_id_confirm_pwd)
	EditText mEtConfirmPwd;
	//完成
	@Bind(R.id.pwd_id_finish)
	Button mBtnFinish;
	
	@Override
	protected void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		Intent intent = getIntent();
		mType = intent.getIntExtra(CstOuer.KEY.TYPE, ESmsType.SMS_TYPE_REGISTER);
		mPhone = intent.getStringExtra(CstOuer.KEY.PHONE);
		mCode = intent.getStringExtra(CstOuer.KEY.CODE);
	}
		
	@Override
	protected void initTop() {
		setNavigation(R.drawable.common_ic_left_nav);

		if(mType == ESmsType.SMS_TYPE_REGISTER) {
			setTitle(R.string.user_register);
		} else {
			setTitle(R.string.user_pwd_forget);
		}
	}

	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_user_pwd);
	}

	@Override
	protected void initViews() {
		ButterKnife.bind(this);

		final int width = UtilDevice.getDevice(this).getWidth();
		final int height = (int)(56 * UtilDevice.getDevice(this).getDensity());

		mBtnFinish.getViewTreeObserver().addOnGlobalLayoutListener(
				new ViewTreeObserver.OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						Rect rect = new Rect();
						mBtnFinish.getWindowVisibleDisplayFrame(rect);
						mBtnFinish.layout(0,
								rect.bottom - rect.top - 2 * height,
								width,
								rect.bottom - rect.top - height);
					}
				});
	}

	@OnClick(R.id.pwd_id_finish)
	public void next() {
		if(mType == ESmsType.SMS_TYPE_REGISTER) {
			register();
		} else {
			forgetPwd();
		}
	}
	
	/**
	 * 注册
	 */
	private void register() {
		String passwd = mEtPasswd.getText().toString().trim();
		String confirmPwd = mEtConfirmPwd.getText().toString().trim();

		//密码为空
		if(UtilString.isBlank(passwd)) {
			UtilOuer.toast(this, R.string.user_passwd_empty);
			mEtPasswd.requestFocus();
			return;
		}

		//密码格式错误
		if(passwd.length() <6 || passwd.length()>16) {
			UtilOuer.toast(this, R.string.user_passwd_error);
			mEtPasswd.requestFocus();
			return;
		}

		//确认密码为空
		if(UtilString.isBlank(confirmPwd)) {
			UtilOuer.toast(this, R.string.user_modify_pwd_confirm_empty);
			mEtConfirmPwd.requestFocus();
			return;
		}

		//两次密码不一致
		if(!passwd.equals(confirmPwd)) {
			UtilOuer.toast(this, R.string.user_modify_pwd_confirm_error);
			mEtConfirmPwd.requestFocus();
			return;
		}

		//重置密码
		AgnettyFuture future = OuerApplication.mOuerFuture.register(
				mPhone, UtilMD5.getStringMD5(passwd), mCode,
				new OuerFutureListener(this) {
					@Override
					public void onStart(AgnettyResult result) {
						super.onStart(result);
						setWaitingDialog(true);
					}

					@Override
					public void onComplete(AgnettyResult result) {
						super.onComplete(result);
						setWaitingDialog(false);
						OuerDispatcher.presentMainTabActivity(UserPwdActivity.this);
						UtilOuer.toast(UserPwdActivity.this, R.string.user_register_success);
						finish();
					}

					@Override
					public void onException(AgnettyResult result) {
						super.onException(result);
						setWaitingDialog(false);
						//异常提示
						Exception ex = result.getException();
						if(ex instanceof OuerException) {
							UtilOuer.toast(UserPwdActivity.this, ex.getMessage());
						} else {
							UtilOuer.toast(UserPwdActivity.this, R.string.user_register_failure);
						}
					}

					@Override
					public void onNetUnavaiable(AgnettyResult result) {
						super.onNetUnavaiable(result);
						setWaitingDialog(false);
					}
				}
		);

		attachDestroyFutures(future);
	}
	
	/**
	 * 忘记密码修改
	 */
	private void forgetPwd() {
		String passwd = mEtPasswd.getText().toString().trim();
		String confirmPwd = mEtConfirmPwd.getText().toString().trim();

		//密码为空
		if(UtilString.isBlank(passwd)) {
			UtilOuer.toast(this, R.string.user_passwd_empty);
			mEtPasswd.requestFocus();
			return;
		}

		//密码格式错误
		if(passwd.length() <6 || passwd.length()>16) {
			UtilOuer.toast(this, R.string.user_passwd_error);
			mEtPasswd.requestFocus();
			return;
		}

		//确认密码为空
		if(UtilString.isBlank(confirmPwd)) {
			UtilOuer.toast(this, R.string.user_modify_pwd_confirm_empty);
			mEtConfirmPwd.requestFocus();
			return;
		}

		//两次密码不一致
		if(!passwd.equals(confirmPwd)) {
			UtilOuer.toast(this, R.string.user_modify_pwd_confirm_error);
			mEtConfirmPwd.requestFocus();
			return;
		}


		//忘记密码
		AgnettyFuture future = OuerApplication.mOuerFuture.forgetPwd(
				mPhone, UtilMD5.getStringMD5(passwd),mCode,
				new OuerFutureListener(this) {

			@Override
			public void onStart(AgnettyResult result) {
				super.onStart(result);
				setWaitingDialog(true);
			}

			@Override
			public void onComplete(AgnettyResult result) {
				super.onComplete(result);
				setWaitingDialog(false);
				UtilOuer.toast(UserPwdActivity.this, R.string.user_forget_pwd_success);
				OuerDispatcher.sendForgetPwdBroadcast(UserPwdActivity.this);
				finish();
			}

			@Override
			public void onException(AgnettyResult result) {
				super.onException(result);
				setWaitingDialog(false);

				Exception ex = result.getException();
				if(ex instanceof OuerException) {
					UtilOuer.toast(UserPwdActivity.this, ex.getMessage());
				}else {
					UtilOuer.toast(UserPwdActivity.this, R.string.user_forget_pwd_failure);
				}
			}

			@Override
			public void onNetUnavaiable(AgnettyResult result) {
				super.onNetUnavaiable(result);
				setWaitingDialog(false);
			}
		});

		attachDestroyFutures(future);
	}

}
